package com.jhws.business.car.controller;

import com.google.zxing.WriterException;
import com.jhws.business.car.bo.CarLane;
import com.jhws.business.car.bo.ParkingLot;
import com.jhws.business.car.service.CarLaneService;
import com.jhws.business.car.service.MNSMessageService;
import com.jhws.business.car.service.ParkingLotService;
import com.jhws.common.base.bo.JsonRespData;

import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.common.pay.service.WXPayService;
import com.jhws.core.exception.BizException;
import com.jhws.util.LogHelper;
import com.jhws.util.QRCodeUtil;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/car/parkingLot")
public class ParkingLotController {
    @Resource
    private ParkingLotService parkingLotService;
    @Resource
    private ITblCommDimService commDimService;
    @Resource
    private MNSMessageService mnsMessageService;
    @Resource
    private CarLaneService carLaneService;
    @Resource
    private WXPayService wxPayService;

    /**
     * 保存停车场
     *
     * @param parkingLot
     * @return
     * @throws Exception
     */
    @RequestMapping("saveParkingLot.x")
    @ResponseBody
    public JsonRespData saveParkingLot(ParkingLot parkingLot) throws Exception {
        LogHelper.d("-------------->    " + parkingLot.toString());
        TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(parkingLot.getCommCode());
        if (commDimBo == null || commDimBo.getCity() == null) {
            return new JsonRespData("", -1, "小区信息错误");
        }
        parkingLotService.saveParkingLot(parkingLot);
        return new JsonRespData("", 1, "");
    }

    /**
     * 保存月卡费率
     */
    @RequestMapping("saveMonthlyRate.x")
    @ResponseBody
    public JsonRespData saveMonthlyRate(String parkId, int monthlyRate) throws Exception {
        parkingLotService.saveMonthlyRate(parkId, monthlyRate);
        return new JsonRespData("", 1, "");
    }

    /**
     * 分配停车场到小区
     *
     * @param parkId
     * @param commCode
     * @return
     */
    @RequestMapping("assignedParkingLot.x")
    @ResponseBody
    public JsonRespData assignedParkingLot(String parkId, String commCode) {
        ParkingLot parkingLot = parkingLotService.queryParkingLotInfo(parkId);
        parkingLot.setCommCode(commCode);
        parkingLotService.saveParkingLot(parkingLot);
        return new JsonRespData("", 1, "");
    }

    /**
     * 创建固定队列
     *
     * @param parkId
     * @param isCreate
     * @return
     * @throws Exception
     */
    @RequestMapping("createFixedQueue.x")
    @ResponseBody
    public JsonRespData createFixedQueue(String parkId, boolean isCreate) throws Exception {
        ParkingLot parkingLot = parkingLotService.queryParkingLotInfo(parkId);
        if (isCreate) {
            mnsMessageService.createQueue(parkId, 30);
            parkingLot.setFixedQueue(1);
        } else {
            mnsMessageService.deleteQueue(parkId);
            parkingLot.setFixedQueue(0);
        }
        parkingLotService.saveParkingLot(parkingLot);
        return new JsonRespData("", 1, "");
    }

    /**
     * 查询停车场列表
     *
     * @param param
     * @return
     */
    @RequestMapping("queryParkingLotList.x")
    @ResponseBody
    public Map<String, Object> queryParkingLotList(@RequestParam Map param) {
        return parkingLotService.queryParkingLotList(param);
    }

    /**
     * 删除停车场
     *
     * @param parkingLot
     * @return
     */
    @RequestMapping("deleteParkingLot.x")
    @ResponseBody
    public JsonRespData deleteParkingLot(ParkingLot parkingLot) {
        parkingLotService.deleteParkingLot(parkingLot);
        return new JsonRespData("", 1, "");
    }

    /**
     * 查询停车场信息
     *
     * @param parkId
     * @return
     */
    @RequestMapping("queryParkingLotInfo.x")
    @ResponseBody
    public JsonRespData queryParkingLotInfo(String parkId) {
        ParkingLot parkingLot = parkingLotService.queryParkingLotInfo(parkId);
        return new JsonRespData("", 1, parkingLot);
    }

    /**
     * 获取预支付二维码
     *
     * @param response
     * @param request
     * @param parkId
     * @param laneCode
     * @throws IOException
     * @throws WriterException
     */
    @RequestMapping("qrcode.x")
    public void createQRCode(HttpServletResponse response, HttpServletRequest request, String parkId, String laneCode) throws Exception {
        ParkingLot parkingLot = parkingLotService.queryParkingLotInfo(parkId);
        if (parkingLot == null || parkingLot.getPayCustomerId() == 0) {
            throw new BizException("-1", "", "停车场参数错误！");
        }
        String domainName = request.getScheme() + "://" + request.getServerName();
        //禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Content-Disposition", "attachment;fileName=qrcode.jpg");
        String webUrl;
        if (StringUtils.isEmpty(laneCode)) {
            webUrl = "/api/v2/car/anon/orderedQrcode?parkId=" + parkId + "&" + "appId=" + wxPayService.getAppId(parkingLot.getPayCustomerId());
        } else {
            List<CarLane> carLanes = carLaneService.queryCarLanes("where park_id='" + parkId + "' and laneCode='" + laneCode + "'");
            if (carLanes.size() > 0) {
                webUrl = "/api/v2/car/anon/laneQrcode?laneId=" + carLanes.get(0).getId() + "&" + "appId=" + wxPayService.getAppId(parkingLot.getPayCustomerId());
            } else {
                throw new BizException("-1", "", "车道不存在！");
            }
        }
        String url = domainName + webUrl;
        ImageIO.write(QRCodeUtil.createImage(url, null, true), "JPEG", response.getOutputStream());
        response.getOutputStream().flush();
    }
}
